package com.xbongbong.saas.service.impl;

import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.helper.PageHelper;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.paas.toolbox.util.CommentUtil;
import com.xbongbong.paas.toolbox.util.DateTimeUtil;
import com.xbongbong.paas.toolbox.util.PageHelperUtil;
import com.xbongbong.pro.dingtalk.pojo.dto.BundleReportGetDTO;
import com.xbongbong.pro.dingtalk.pojo.dto.BundleReportListDTO;
import com.xbongbong.pro.dingtalk.pojo.vo.BundleReportGetVO;
import com.xbongbong.pro.dingtalk.pojo.vo.BundleReportListVO;
import com.xbongbong.pro.dingtalk.pojo.vo.BundleReportVO;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.saas.domain.entity.DingReportEntity;
import com.xbongbong.saas.enums.mini.DingReportTypeEnum;
import com.xbongbong.saas.model.DingReportModel;
import com.xbongbong.saas.service.DingReportService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 应用模块名称<p>
 * 代码描述<p>
 * Company: 逍邦网络科技有限公司<p>
 *
 * @author chenshan
 * @version v1.0
 * @since 2020/9/1 9:13 下午
 */
@Service("dingReportService")
public class DingReportServiceImpl implements DingReportService {
    @Resource
    private DingReportModel dingReportModel;
    
    @Override
    public BundleReportListVO list(BundleReportListDTO bundleReportListDTO) throws XbbException {
        Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        param.put("del", DelEnum.NORMAL.getDel());
        param.put("corpid", bundleReportListDTO.getCorpid());
        param.put("openConversationId", bundleReportListDTO.getOpenConversationId());
        CommentUtil.addToMap(param, "orderByStr", "id desc");
        CommentUtil.addToMap(param, "page", bundleReportListDTO.getPage());
        PageHelper pageHelper = PageHelperUtil.getPageHelper(param, dingReportModel, bundleReportListDTO.getPageSize());
        List<DingReportEntity> list = (List<DingReportEntity>) PageHelperUtil.getEntityList(param, pageHelper, dingReportModel);
        BundleReportListVO bundleReportListVO = new BundleReportListVO();
        List<BundleReportVO> bundleReportVOList = new ArrayList<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        list.forEach(dingReportEntity -> {
            Long recordTime = dingReportEntity.getRecordTime();
            Integer type = dingReportEntity.getType();
            DingReportTypeEnum dingReportTypeEnum = DingReportTypeEnum.getByCode(type);
            String recordTimeStr = DateTimeUtil.getDateStringEpochSecond(recordTime);
            String title = recordTimeStr + dingReportTypeEnum.getDescribe();
            BundleReportVO bundleReportVO = new BundleReportVO();
            BeanUtil.copyProperties(dingReportEntity,bundleReportVO);
            bundleReportVO.setDataId(dingReportEntity.getId());
            bundleReportVO.setTitle(title);
            bundleReportVOList.add(bundleReportVO);
        });
        bundleReportListVO.setList(bundleReportVOList);
        bundleReportListVO.setPageHelper(pageHelper);
        return bundleReportListVO;
    }
    
    @Override
    public BundleReportGetVO get(BundleReportGetDTO bundleReportGetDTO) throws XbbException {
        DingReportEntity dingReportEntity = dingReportModel.getEntity(bundleReportGetDTO.getDataId(),bundleReportGetDTO.getCorpid());
    
        if ( Objects.isNull(dingReportEntity) ) {
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100028,"数据不存在");
        }
        Long recordTime = dingReportEntity.getRecordTime();
        Integer type = dingReportEntity.getType();
        DingReportTypeEnum dingReportTypeEnum = DingReportTypeEnum.getByCode(type);
        String recordTimeS = DateTimeUtil.getStringEpochSecond(recordTime);
//        String recordTimeStr = DateTimeUtil.getStartOfHour(recordTime);
        String recordTimeStr = DateTimeUtil.getDateStringEpochSecond(recordTime);
        String title = recordTimeStr + dingReportTypeEnum.getDescribe();
    
        BundleReportGetVO bundleReportGetVO = new BundleReportGetVO();
        BeanUtil.copyProperties(dingReportEntity,bundleReportGetVO);
        bundleReportGetVO.setSlogan(dingReportEntity.getName()+dingReportTypeEnum.getDescribe());
        bundleReportGetVO.setDataId(dingReportEntity.getId());
        bundleReportGetVO.setFinishNum(dingReportEntity.getFinishNum());
        bundleReportGetVO.setTitle(title);
        bundleReportGetVO.setRecordTime(recordTimeS);
        return bundleReportGetVO;
    }
}
